//
//  ZYCStuAttendanceStatisticsDetailViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/27.
//

#import "ZYCStuAttendanceStatisticsDetailViewController.h"
#import "ZYCTourRecordMenuView.h"
#import "ZYCStuAttendanceStatistDetailCell.h"
#import "ZYCAttendanceStatusStatisticsView.h"
#import "ZYCAttendanceStatisticsModel.h"
#import "ZYCCommonBaseModel.h"
#import "ZYCCommonSelectObjectPickerView.h"
#import "ZYCSelectPastDateView.h"
#import "UITableView+NoData.h"

static NSInteger pageSize = 10;
@interface ZYCStuAttendanceStatisticsDetailViewController ()<UITableViewDelegate,UITableViewDataSource,ZYCTourRecordMenuViewDelegate,ZYCCommonSelectObjectPickerViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ZYCTourRecordMenuView *menuView;
@property (nonatomic, strong) NSArray *gradeArr;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) ZYCAttendanceStatusStatisticsView *headerView;
@property (nonatomic, assign) NSInteger pageNum;

@property (nonatomic, strong) ZYCSelectPastDateView *dateSelectView;
@property (nonatomic, strong) ZYCCommonSelectObjectPickerView *classPickerView;

@property (nonatomic, copy) NSString *grade;
@property (nonatomic, copy) NSString *classId;



@end

@implementation ZYCStuAttendanceStatisticsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.model.gradeName;
    [self.view addSubview:self.menuView];
    [self.view addSubview:self.tableView];
    self.menuView.leftStr = self.attendanceDate;
    self.tableView.tableHeaderView = self.headerView;
//    self.headerView.model = self.model;
    
    [self getClassListWithModel:self.model];
    [self loadStuAttendnceStatistics];

}

- (void)loadStuAttendnceStatistics{
    NSDictionary *params = @{
        @"attendanceDate":self.attendanceDate ? self.attendanceDate : @"",
        @"classId":self.classId ? self.classId : @"",
        @"grade":self.model.grade
    };
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_AttendanceRecord requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            if (SUCCESS(responseObject[@"code"])) {
                self.headerView.countZC = responseObject[@"body"][@"countZC"];
                self.headerView.countCD = responseObject[@"body"][@"countCD"];
                self.headerView.countQJ = responseObject[@"body"][@"countQJ"];
                self.headerView.countWD = responseObject[@"body"][@"countWD"];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}

// 根据年级获取班级
- (void)getClassListWithModel:(ZYCAttendanceStatisticsModel *)model{
    NSDictionary *params = @{
        @"grade":model.grade
    };
    [[URLManager sharedURLManager] getUrlString:Common_Post_QueryClassListByGrade requestService:RequestServiceWorkBench parameters:params progress:nil success:^(id  _Nullable responseObject) {
        if (SUCCESS(responseObject[@"code"])) {
            NSMutableArray *temp = [NSMutableArray arrayWithCapacity:0];
            ZYCCommonBaseModel *first = [ZYCCommonBaseModel new];
            first.name = @"全部班级";
            first.nameId = @"";
            [temp addObject:first];
            NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCCommonBaseModel class] json:responseObject[@"body"]];
            [temp addObjectsFromArray:array];
            self.menuView.rightStr = first.name;
            self.classPickerView.dataSource = temp;
            [self.classPickerView reload];
            
            [self loadNewData];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}


// 获取班级考勤统计
- (void)loadNewData{
    self.pageNum = 1;
    NSDictionary *params = @{
        @"pageNum":@(self.pageNum),
        @"pageSize":@(pageSize),
        @"attendanceDate":self.attendanceDate ? self.attendanceDate : @"",
        @"grade":self.model.grade,
        @"classId":self.classId ? self.classId : @""
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_ClassAttendanceCensus requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(id  _Nullable responseObject) {
        [MrjSVProgressHUD dismiss];
        if (SUCCESS(responseObject[@"code"])) {
            if (SUCCESS(responseObject[@"code"])) {
                [self.dataSource removeAllObjects];
                NSArray *array  =[NSArray yy_modelArrayWithClass:[ZYCAttendanceStatisticsModel class] json:responseObject[@"body"][@"list"]];
                [self.dataSource addObjectsFromArray:array];
                [self.tableView showDataCount:self.dataSource.count];

                [self.tableView reloadData];
                // 每次下拉刷新需要重置一下
                [self reset];
                [self.tableView.mj_header endRefreshing];
            }
        }else{
            [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
        }
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)loadMoreData{
    self.pageNum ++;
    NSDictionary *params = @{
        @"pageNum":@(self.pageNum),
        @"pageSize":@(pageSize),
        @"attendanceDate":self.attendanceDate ? self.attendanceDate : @"",
        @"grade":self.model.grade,
        @"classId":self.classId ? self.classId : @""
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_ClassAttendanceCensus requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(id  _Nullable responseObject) {
        [MrjSVProgressHUD dismiss];
        if (SUCCESS(responseObject[@"code"])) {
            NSArray *array  =[NSArray yy_modelArrayWithClass:[ZYCAttendanceStatisticsModel class] json:responseObject[@"body"][@"list"]];
            if (array.count == 0) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.dataSource addObjectsFromArray:array];
                [self.tableView reloadData];
                [self.tableView.mj_footer endRefreshing];
            }
        }else{
            [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
        }
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)reset{
    [self.tableView.mj_footer setRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.tableView.mj_footer resetNoMoreData];
}


#pragma mark - ZYCTourRecordMenuViewDelegate
- (void)tourRecordMenuViewClickLeft{
    [self.dateSelectView show];
}
- (void)tourRecordMenuViewClickRight{
    [self.classPickerView show];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCStuAttendanceStatistDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCStuAttendanceStatistDetailCell class]) forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.section];
    [cell setSectionCircularTableView:tableView withIndexPath:indexPath];
    return cell;

}
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 103.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

}


#pragma mark - ZYCCommonSelectObjectPickerViewDelegate
- (void)selectObjectPickerView:(ZYCCommonSelectObjectPickerView *)pickerView DidClickModel:(ZYCCommonBaseModel *)model{
    self.classId = model.nameId;
    self.menuView.rightStr = model.name;
    [self loadNewData];
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}


- (ZYCAttendanceStatusStatisticsView *)headerView {
    if (!_headerView) {
        _headerView = [[ZYCAttendanceStatusStatisticsView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 150)];
        _headerView.backgroundColor = [UIColor clearColor];
    }
    return _headerView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.menuView.frame), kScreenWidth, kScreenHeight - CGRectGetMaxY(self.menuView.frame)) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCStuAttendanceStatistDetailCell class] forCellReuseIdentifier:NSStringFromClass([ZYCStuAttendanceStatistDetailCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];

    }
    return _tableView;
}

- (ZYCTourRecordMenuView *)menuView {
    if (!_menuView) {
        _menuView = [[ZYCTourRecordMenuView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, 46.0f)];
        _menuView.delegate = self;
    }
    return _menuView;
}


- (ZYCCommonSelectObjectPickerView *)classPickerView {
    if (!_classPickerView) {
        _classPickerView = [[ZYCCommonSelectObjectPickerView alloc] init];
        _classPickerView.tipString = @"请选择班级";
        _classPickerView.delegate = self;
        [self.view addSubview:_classPickerView];
    }
    return _classPickerView;
}

- (ZYCSelectPastDateView *)dateSelectView {
    ZYCWeakSelf
    if (!_dateSelectView) {
        _dateSelectView = [[ZYCSelectPastDateView alloc] init];
        _dateSelectView.tipString = @"请选择时间";
        _dateSelectView.currentDate = [NSDate date];
        _dateSelectView.confirmAction = ^(NSString * _Nonnull dateStr) {
            weakSelf.attendanceDate = dateStr;
            weakSelf.menuView.leftStr = dateStr;
            [weakSelf loadStuAttendnceStatistics];
            [weakSelf loadNewData];
        };
        [self.view addSubview:_dateSelectView];
    }
    return _dateSelectView;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
